#include "paramtypedelegate.h"
#include "global.h"

ParamTypeDelegate::ParamTypeDelegate(QObject* parent):
    QItemDelegate(parent)
{
    m_typeList << Global::instance()->paramTypeStringList();
}

ParamTypeDelegate::~ParamTypeDelegate()
{

}

void ParamTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QItemDelegate::paint(painter, option, index);
}

QSize ParamTypeDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QItemDelegate::sizeHint(option, index);
}

QWidget *ParamTypeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid() && index.column() == COMBOXCOL)
    {
        QComboBox *editor = new QComboBox(parent);
        editor->setEditable(false);
        editor->installEventFilter(const_cast<ParamTypeDelegate*>(this));
        return editor;
    }
    else
    {
        return QItemDelegate::createEditor(parent, option, index);
    }
}

void ParamTypeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if(index.isValid() && index.column() == COMBOXCOL)
    {
        QString value = index.model()->data(index, Qt::DisplayRole).toString();
        QComboBox* combox = static_cast<QComboBox*>(editor);
        combox->addItems(m_typeList);

        combox->setCurrentText(value.isEmpty()?m_typeList.at(0):value);
    }
    else
    {
        return QItemDelegate::setEditorData(editor, index);
    }
}

void ParamTypeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if(index.isValid() && index.column() == COMBOXCOL)
    {
        QComboBox* combox = static_cast<QComboBox*>(editor);
        QString value = combox->currentText();

        model->setData(index, value.isEmpty()?m_typeList.at(0):value);
    }
    else
    {
        return QItemDelegate::setModelData(editor, model, index);
    }
}
